package bitmapdata
{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Point;
	
	public class BitmapFunctionTest extends Sprite
	{
		private var bitmap1:Bitmap;
		private var bitmapData1:BitmapData;
		
		private var bitmap2:Bitmap;
		private var bitmapData2:BitmapData;
		
//		[Embed(source="assets/general_13_1.png")]
		[Embed(source="assets/6.png")]
		private var pic1:Class;
//		[Embed(source="assets/general_20_1.png")]
		[Embed(source="assets/10.png")]
		private var pic2:Class
		private var _pixelCount:int = 0;
		private var _seed:Number;
		
		public function BitmapFunctionTest()
		{
			super();
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
//			noise();
//			pixelDissolve();
//			randomArr();
			myDissolve();
		}
		
		public function noise():void {
			bitmapData1 = new BitmapData(100,100,true,0xfffff000);
			bitmap1 = new Bitmap(bitmapData1);
			this.addChild(bitmap1);
			bitmapData1.noise(Math.random()*1000,0,255,BitmapDataChannel.RED,true);
		}
		
		public function pixelDissolve():void {
			bitmap1 = new pic1() as Bitmap;
			bitmap2 = new pic2() as Bitmap;
			bitmapData1 = bitmap1.bitmapData;
			bitmapData2 = bitmap2.bitmapData;
			this.addChild(bitmap2);
			this.addChild(bitmap1);
			
			_seed = Math.random()*100;
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		public function onEnterFrame(event:Event):void {
			_seed = bitmapData1.pixelDissolve(bitmapData1,bitmapData1.rect,new Point(),_seed,200,0x00ffffff);
			_pixelCount += 200;
			if(_pixelCount>bitmapData1.width*bitmapData1.height) {
				this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
		}
		
		
		private var myArr:Array = new Array();
		private var index:int = 0;
		
		public function myDissolve():void {
			bitmap1 = new pic1() as Bitmap;
			bitmap2 = new pic2() as Bitmap;
			bitmapData1 = bitmap1.bitmapData;
			bitmapData2 = bitmap2.bitmapData;
			this.addChild(bitmap2);
			this.addChild(bitmap1);
			
			for(var i:int=0;i<bitmapData1.width;i++) {
				for(var j:int=0;j<bitmapData1.height;j++) {
					myArr.push(new Point(i,j));
				}
			}
			
//			myArr = ArrRandom(myArr);
			
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame2);
		}
		
		public function onEnterFrame2(event:Event):void {
			var max:int = Math.min(index+200,myArr.length-1);
			for(var i:int=index;i<max;i++) {
				var point:Point = myArr[i] as Point;
				trace(point+"............................................."+bitmapData1+"............................"+index+"......................."+myArr.length);
				bitmapData1.setPixel32(point.x,point.y,0x00ffffff);
				index++;
				if(index>=(myArr.length-1)) {
					this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				}
			}
		}
		
		//用到的两个函数
		
		private function ArrRandom(OArr:Array ):Array {
			var TArr:Array =OArr;
			var NArr:Array =new Array();
			var TArrLen:Number =TArr.length
			for (var i:int=0;i<TArrLen;i++){
				var shengyu:Number =TArr.length-1 ;
				var newid:Number   =i;
				var oldid:Number =Math.random()*shengyu;
				oldid=oldid-oldid%1;
				NArr[newid]=TArr[oldid];
				TArr=DEL(TArr,oldid);
			}
			return NArr;
		}
		private function DEL(Arr:Array ,id:Number ):Array {
			var ArrLen:int=Arr.length;
			for (var j:int=id;j<ArrLen;j++){
				Arr[j]=Arr[j+1];
			}
			Arr.pop();
			return Arr;
		} 
		

	}
}